﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using PESWebMVC.Models;

namespace PESWebMVC.Controllers
{
    using System.ServiceModel.Web;

    public class CampeonatoController : ApiController
    {
        private readonly PESDataBaseContext m_db = new PESDataBaseContext();

        // GET api/Campeonato
        public IEnumerable<Campeonato> GetCampeonatoes()
        {
            return m_db.CampeonatoSet.AsEnumerable();
        }

        // GET api/Campeonato/5
        [WebGet(UriTemplate = "Campeonato/{id}",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        public Campeonato GetCampeonato(int id)
        {
            Campeonato campeonato = m_db.CampeonatoSet.Find(id);
            if (campeonato == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return campeonato;
        }

        // PUT api/Campeonato/5
        public HttpResponseMessage PutCampeonato(int id, Campeonato campeonato)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != campeonato.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            m_db.Entry(campeonato).State = EntityState.Modified;

            try
            {
                m_db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Campeonato
        public HttpResponseMessage PostCampeonato(Campeonato campeonato)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
           
            m_db.CampeonatoSet.Add(campeonato);
            m_db.SaveChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, campeonato);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new {id = campeonato.Id}));
            return response;
        }

        // DELETE api/Campeonato/5
        public HttpResponseMessage DeleteCampeonato(int id)
        {
            Campeonato campeonato = m_db.CampeonatoSet.Find(id);
            if (campeonato == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            m_db.CampeonatoSet.Remove(campeonato);

            try
            {
                m_db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, campeonato);
        }

        protected override void Dispose(bool disposing)
        {
            m_db.Dispose();
            base.Dispose(disposing);
        }
    }
}